package com.ruoyi.comparison.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.comparison.domain.Domains;
import com.ruoyi.comparison.domain.TaskSubjectList;
import com.ruoyi.comparison.domain.bo.TaskSubjectListBo;
import com.ruoyi.comparison.domain.vo.TaskSubjectListVo;
import com.ruoyi.comparison.mapper.TaskSubjectListMapper;
import com.ruoyi.comparison.service.ITaskSubjectListService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 同一条关联关系上的节点信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-18
 */
@RequiredArgsConstructor
@Service
public class TaskSubjectListServiceImpl implements ITaskSubjectListService {

    private final TaskSubjectListMapper baseMapper;

    /**
     * 查询同一条关联关系上的节点信息
     */
    @Override
    public TaskSubjectListVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询同一条关联关系上的节点信息列表
     */
    @Override
    public TableDataInfo<TaskSubjectListVo> queryPageList(TaskSubjectListBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TaskSubjectList> lqw = buildQueryWrapper(bo);
        String username = LoginHelper.getUsername();
        lqw.eq(TaskSubjectList::getCreateBy,username);
        Page<TaskSubjectListVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询同一条关联关系上的节点信息列表
     */
    @Override
    public List<TaskSubjectListVo> queryList(TaskSubjectListBo bo) {
        LambdaQueryWrapper<TaskSubjectList> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TaskSubjectList> buildQueryWrapper(TaskSubjectListBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TaskSubjectList> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getNodeId()), TaskSubjectList::getNodeId, bo.getNodeId());
        lqw.eq(StringUtils.isNotBlank(bo.getKeyNo()), TaskSubjectList::getKeyNo, bo.getKeyNo());
        lqw.like(StringUtils.isNotBlank(bo.getName()), TaskSubjectList::getName, bo.getName());
        return lqw;
    }

    /**
     * 新增同一条关联关系上的节点信息
     */
    @Override
    public Boolean insertByBo(TaskSubjectListBo bo) {
        TaskSubjectList add = BeanUtil.toBean(bo, TaskSubjectList.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改同一条关联关系上的节点信息
     */
    @Override
    public Boolean updateByBo(TaskSubjectListBo bo) {
        TaskSubjectList update = BeanUtil.toBean(bo, TaskSubjectList.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TaskSubjectList entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除同一条关联关系上的节点信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public void insert(List<TaskSubjectList> list) {
        this.baseMapper.insertBatch(list);
    }

    @Override
    public TableDataInfo<TaskSubjectListVo> queryPageList1(TaskSubjectListBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TaskSubjectList> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(TaskSubjectList::getCreateTime);
        String username = LoginHelper.getUsername();
        lqw.eq(TaskSubjectList::getCreateBy,username);
        lqw.groupBy(TaskSubjectList::getSubjectId);
        Page<TaskSubjectListVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public List<TaskSubjectListVo> findAllKey() {
        LambdaQueryWrapper<TaskSubjectList> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(TaskSubjectList::getCreateTime);
        String username = LoginHelper.getUsername();
        wrapper.eq(TaskSubjectList::getCreateBy,username);
        List<TaskSubjectList> list = this.baseMapper.selectList(wrapper);
        List<TaskSubjectListVo> collect = list.stream().map(taskSubjectList -> {
            TaskSubjectListVo taskSubjectListVo = new TaskSubjectListVo();
            taskSubjectListVo.setSearchKey1(taskSubjectList.getSearchKey1());
            taskSubjectListVo.setSearchKey2(taskSubjectList.getSearchKey2());
            taskSubjectListVo.setCreateTime(taskSubjectList.getCreateTime());
            taskSubjectListVo.setSubjectId(taskSubjectList.getSubjectId());
            return taskSubjectListVo;
        }).distinct().collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<TaskSubjectListVo> findKey(List<Domains> domains) {
        List<TaskSubjectListVo> list = new ArrayList<>();
        int a=1;
        for (int m = 0; m < domains.size()-1; m++) {
            for (int n = a; n < domains.size(); n++) {
                String searchKey1 = domains.get(m).getValue();
                String searchKey2 = domains.get(n).getValue();
                TaskSubjectListVo taskSubjectListVo = new TaskSubjectListVo();
                taskSubjectListVo.setSearchKey1(searchKey1);
                taskSubjectListVo.setSearchKey2(searchKey2);
                list.add(taskSubjectListVo);
            }
            a++;
        }
        System.out.println(list);
        return list;
    }

    @Override
    public TaskSubjectList findOne(String nodeId) {
        LambdaQueryWrapper<TaskSubjectList> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TaskSubjectList::getNodeId,nodeId);
        List<TaskSubjectList> list = this.baseMapper.selectList(wrapper);
        return list.get(0);
    }

}
